// Fill out your copyright notice in the Description page of Project Settings.

#include "Isalong2.h"
#include "GunData.h"
#include "CPlayerCharacter.h"
#include "BowCustomLogic.h"




UBowCustomLogic::UBowCustomLogic()
{
	_override_OnFireBegin = true;
	_override_OnFireEnd = true;
	_override_OnFireAnimationEnd = true;
	_override_OnFireAnimationFired = true;
	_override_OnReloadEnd = true;
	_useLogicTick = true;
	PrimaryComponentTick.bCanEverTick = false;
}

void UBowCustomLogic::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
	Super::TickComponent(DeltaTime, TickType, ThisTickFunction);

}

void UBowCustomLogic::OnFireBegin()
{
	if (!_reloadComplete) { return; }
	FWeaponPack data=_compOwner->GetWeaponPackData();
	_compOwner->GetWeaponOwner()->OnPlayMontage(data._characterFireMontage);
	_compOwner->GetSKMesh()->GetAnimInstance()->Montage_Play(data._weaponReloadMontage);
	_useLogicTick = true;
	_force = 1.0f;
}

void UBowCustomLogic::OnFireEnd()
{
	if (!_reloadComplete) { return; }
	_compOwner->GetWeaponOwner()->OnStopMontage(0.2f);
	FWeaponPack data = _compOwner->GetWeaponPackData();
	_compOwner->GetSKMesh()->GetAnimInstance()->Montage_Play(data._weaponFireMontage);
	if (_holdArrow!=nullptr)
	{
		_holdArrow->Fire(_compOwner->GetWeaponPackData()._baseHurtValue, 100000*_force,Cast<AActor>(_compOwner->GetWeaponOwner()));
	}
	_compOwner->SetBulletContains(0, true);
	_holdArrow = nullptr;
	_useLogicTick = false;
}

void UBowCustomLogic::OnReloadEnd(int count)
{
	_compOwner->SetBulletContains(_compOwner->GetBulletContains() + count, true);
	if (_compOwner->GetWeaponOwner() != nullptr)
	{
		_compOwner->GetWeaponOwner()->OnBulletChangeCallBack();
	}
	//ArrowPlace
	_holdArrow=_compOwner->GetWorld()->SpawnActor<ABulletArrow>(ABulletArrow::StaticClass(), _compOwner->GetActorLocation(), FRotator(0, 0, 0));
	_holdArrow->AttachRootComponentToActor(_compOwner, TEXT("ArrowPlace"),EAttachLocation::SnapToTarget);
	_reloadComplete = true;
}

void UBowCustomLogic::OnFireAnimationEnd()
{
	FWeaponPack data = _compOwner->GetWeaponPackData();
	_compOwner->GetSKMesh()->GetAnimInstance()->Montage_Play(data._weaponReloadMontage);
	_compOwner->GetSKMesh()->GetAnimInstance()->Montage_SetPosition(data._weaponReloadMontage, 0.92);
	_compOwner->GetSKMesh()->GetAnimInstance()->Montage_Pause();
	_compOwner->GetWeaponOwner()->OnPlayMontage(data._characterADSMontage);
}

void UBowCustomLogic::OnFireAnimationFired()
{
	FWeaponPack data = _compOwner->GetWeaponPackData();
	_compOwner->GetWeaponOwner()->OnPlayMontage(data._characterADSMontage);
}

// for arrow attach
void UBowCustomLogic::LogicTick(float DeltaTime)
{
	if (happened==false)
	{
		gameTime += DeltaTime;
		if (gameTime>0.05)
		{
			happened = true;
			// logic here
			if (_holdArrow==nullptr&&_compOwner->GetBulletContains()>0)
			{
				_holdArrow = _compOwner->GetWorld()->SpawnActor<ABulletArrow>(ABulletArrow::StaticClass(), _compOwner->GetActorLocation(), FRotator(0, 0, 0));
				_holdArrow->AttachRootComponentToActor(_compOwner, TEXT("ArrowPlace"), EAttachLocation::SnapToTarget);
			}
			_useLogicTick = false;
		}
	}
	_force = _force + DeltaTime> 2 ? 2 : _force + DeltaTime;
	ACGameManager::_gameManager->WriteLine(_force);
}

void UBowCustomLogic::OnReloadBegin_Addition()
{
	_reloadComplete = false;
}

void UBowCustomLogic::PutDown_Addition(IWeaponOwner* character)
{
	if (_holdArrow!=nullptr)
	{
		_holdArrow->Destroy();
		_holdArrow = nullptr;
	}
}
